package com.hsh.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.erp.domain.common.Result;
import com.hsh.erp.domain.dto.ProductDTO;
import com.hsh.erp.domain.dto.ShopProductDTO;
import com.hsh.erp.domain.entity.ShopProduct;
import com.hsh.erp.domain.vo.ShopProductVO;
import com.hsh.erp.mapper.ShopProductMapper;
import com.hsh.erp.service.FileService;
import com.hsh.erp.service.ProductService;
import com.hsh.erp.service.ShopProductService;
import com.hsh.erp.service.ShopService;
import com.hsh.erp.utils.MyBeanUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author USER
 * @description 针对表【shop_product】的数据库操作Service实现
 * @createDate 2024-05-30 16:21:43
 */
@Service
public class ShopProductServiceImpl extends ServiceImpl<ShopProductMapper, ShopProduct>
        implements ShopProductService {
    @Resource
    private ProductService productService;
    @Resource
    private ShopService shopService;
    @Resource
    private FileService fileService;

    /**
     * 列表查询
     *
     * @param shopProductVO
     * @return
     */
    @Override
    public Result<List<ShopProductDTO>> getList(ShopProductVO shopProductVO) {
        // 查询所有店铺商品
        Wrapper wrapper = new QueryWrapper<>()
                .like(Strings.isNotBlank(shopProductVO.getSkuNo()), "sku_no", shopProductVO.getSkuNo())
                .eq(shopProductVO.getShopId() != null, "shop_id", shopProductVO.getShopId())
                .eq(shopProductVO.getIsDelete() != null, "is_delete", shopProductVO.getIsDelete());
        List<ShopProductDTO> shopProductDTOList
                = MyBeanUtils.copyList(list(wrapper), ShopProductDTO.class);

        initShopProductDTO(shopProductDTOList);
        // 判断是否需要合并
        if (shopProductVO.getMergeState() == null || shopProductVO.getMergeState() != 1) {
            shopProductDTOList = mergeShopProductDTO(shopProductDTOList);
        }
        return Result.success(shopProductDTOList);
    }

    /**
     * 修改一条记录
     *
     * @param shopProductVO
     * @return
     */
    @Override
    public Result updateOne(ShopProductVO shopProductVO) {
        boolean update = updateById(MyBeanUtils.copy(shopProductVO, ShopProduct.class));
        return Result.create(update);
    }

    /**
     * 删除一条记录
     *
     * @param shopProductVO
     * @return
     */
    @Override
    public Result deleteOne(ShopProductVO shopProductVO) {
        return updateOne(shopProductVO);
    }

    /**
     * 新增一条记录
     *
     * @param shopProductVO
     * @return
     */
    @Override
    public Result addOne(ShopProductVO shopProductVO) {
        boolean save = save(MyBeanUtils.copy(shopProductVO, ShopProduct.class));
        return Result.create(save);
    }
    //============================================私有方法开始============================================

    /**
     * 设置 商品信息 店铺信息
     *
     * @param shopProductDTOList
     * @return
     */
    private void initShopProductDTO(List<ShopProductDTO> shopProductDTOList) {
        for (ShopProductDTO shopProductDTO : shopProductDTOList) {
            // 获得商品信息
            String productName = productService.getNameById(shopProductDTO.getProductId());
            shopProductDTO.setProductInfo(productName + "*" + shopProductDTO.getNum());
            // 获得店铺信息
            shopProductDTO.setShopName(shopService.getNameById(shopProductDTO.getShopId()));
        }
    }

    /**
     * 以 shopId+skuNO 为 key，shopProductDTO 为 value，合并相同Sku编码的信息
     *
     * @param shopProductDTOList
     * @return
     */
    private List<ShopProductDTO> mergeShopProductDTO(List<ShopProductDTO> shopProductDTOList) {
        Map<String, ShopProductDTO> shopProductMap = new HashMap();
        for (ShopProductDTO shopProductDTO : shopProductDTOList) {
            // 以 shopId+skuNO 为 key，shopProductDTO 为 value，合并相同Sku编码的信息
            String key = shopProductDTO.getShopId() + "-" + shopProductDTO.getSkuNo();
            if (shopProductMap.containsKey(key)) {
                // 如果 key 已存在，修改原有数据中的 productInfo
                ShopProductDTO resultShopProductDTO = shopProductMap.get(key);
                resultShopProductDTO.setProductInfo(resultShopProductDTO.getProductInfo() + ";" + shopProductDTO.getProductInfo());
            } else {
                // 如果 key 不存在，直接添加到 map 中
                shopProductMap.put(key, shopProductDTO);
            }
        }
        return new ArrayList<>(shopProductMap.values());
    }

    /**
     * 商品信息只有单个商品时，允许修改
     * @param shopProductVO
     */
    private void getOneProductDTOByStr(ShopProductVO shopProductVO) {
        List<ProductDTO> productDTOList = fileService.getProductDTOListByStr(shopProductVO.getProductInfo());
        if (!productDTOList.isEmpty() && productDTOList.size() == 1) {
            ProductDTO productDTO = productDTOList.get(0);
            shopProductVO.setProductId(productDTO.getId()).setNum(productDTO.getNum());
        }
    }
    //============================================私有方法结束============================================
}




